package SinaDB;
use strict;
use Perl6::Say;
use DBI;
use DBD::mysql;
use Encode qw/ encode decode /;
use Tools;

sub do_new
{
    my ($class,@arg) = @_;
    my $db;
    my $self = bless{
        'db'=>$db,
    },$class;
}

sub connectdb{
    my ($self,$database,$host,$user,$passwd) = @_;
    $$self{'db'} = DBI->connect("DBI:mysql:database=$database;host=$host",$user,$passwd,{'RaiseError' => 1});
}

sub disconnectdb{
    my ($self) = @_;
    $$self{'db'}->disconnect();
}

sub insert_user{
    my ($self,$id) = @_;
    my $_sqr;
    $_sqr = $$self{'db'}->prepare("select id from users where id=$id");
    $_sqr->execute();
    if(not $_sqr->rows){
        $$self{'db'}->do("insert into users set id=$id");
        return 1;
    }
    return 0;
}


sub do{
    my ($self,$querystr) = @_;
    my $_sqr = $$self{'db'}->do($querystr); 
}
sub update{
    my ($self,$table,$id,$args) = @_;
    my $_sqr = $$self{'db'}->do("update ".$table." set ".$args." where id=$id"); 
}
sub set_user_info{
	my ($self,$userid,$user_info) = @_;
	my $_sqr = $$self{'db'}->do("update users set 
					name ='".$$user_info{'name'}."',
					sex ='".$$user_info{'sex'}."',
					area ='".$$user_info{'area'}."',
                    fans_num = '".$$user_info{'fans_num'}."',
					vip_verify='".$$user_info{'vip_verify'}."',
					org_blog_num='".$$user_info{'org_blog_num'}."' 
					where id=$userid");
}
sub select_one{
    my ($self,$table,$args) = @_;
    my $_sqr;
    $_sqr = $$self{'db'}->prepare("select * from ".$table." where ".$args." limit 1");
    $_sqr->execute();
    my $ref = $_sqr->fetchrow_hashref();
    return $ref;
}

sub get_host4grab{
    my ($self) = @_;
    my $_sqr;

    $_sqr = $$self{'db'}->prepare("select * from hosts 
        where grab_rounds=0 order by grab_last_time");
    $_sqr->execute();
    if(!$_sqr->rows){
        $_sqr = $$self{'db'}->prepare("select * from hosts 
        order by grab_last_time ");
        $_sqr->execute(); 
    }
    my $ref = $_sqr->fetchrow_hashref();
    return $ref;
}
sub get_user_need_check{
	my ($self,$start) = @_;
	if($start){
		$start .=','; 
	}else{
		$start = '';
	}
    my $_sqr = $$self{'db'}->prepare("select * from users where name='' or sex='' or area='' or vip_verify=-1 or org_blog_num=-1 or fans_num=-1 limit ".$start."1");
     $_sqr->execute(); 
    my $ref = $_sqr->fetchrow_hashref();
    return $$ref{'id'};
}
sub get_myhost4unfollow{
    my ($self) = @_;
    
    my $_sqr;
    $_sqr = $$self{'db'}->prepare("select * from myhosts where
        vpn is not null and datediff(now(),gtime)>0 limit 1");
    $_sqr->execute();
    my $ref = $_sqr->fetchrow_hashref();
    return $ref;
}
sub get_myhost4follow{
    my ($self) = @_;
    my $_sqr;
    $_sqr = $$self{'db'}->prepare("select * from myhosts where vpn is not null and datediff(now(),gtime)>0");
    $_sqr->execute();
    my $ref = $_sqr->fetchrow_hashref();
    return $ref;
}

sub update_host_after_follow {
    my ($self,$host_id,$unfollow_num) = @_;

    my $_sqr;
    $_sqr = $$self{'db'}->prepare("update myhosts set 
        gtime  =   now(),
        where id=$host_id");
    $_sqr->execute;
}
sub update_host_after_grab {
    my ($self,$host_id,$grab_last_ids,$grab_last_num) = @_;
    
    my $_sqr;
    $_sqr = $$self{'db'}->prepare("update hosts set 
        grab_rounds     =   grab_rounds+1,
        grab_last_time  =   now(),
        grab_last_num   =   $grab_last_num,
        grab_last_ids   =   '$grab_last_ids'
        where id=$host_id");
    $_sqr->execute;
}

sub update_host_after_unfollow {
    my ($self,$host_id,$unfollow_num) = @_;

    my $_sqr;
    $_sqr = $$self{'db'}->prepare("update hosts set 
        unfollow_last_time  =   now(),
        unfollow_num = $unfollow_num
        where id=$host_id");
    $_sqr->execute;
}



# 1 follow 2 unfollow 0 dont know
#sub update_follow{
    #my($self,$user_id,$host_id,$user_follow,$host_follow) = @_;
        
    #my $_sqr;
    #my $_rows;
    #my $_ref;
    #my $_follow_qr_str;
    
    #$_follow_qr_str = 'host_action_time = now()';
    #if($user_follow){
        #$_follow_qr_str .= ",user_follow = $user_follow"
    #}
    #if($host_follow){
        #$_follow_qr_str .= ",host_follow = $host_follow"
    #}

    #$_sqr = $$self{'db'}->prepare("select id from follows 
        #where user_id=$user_id and host_id=$host_id");
    #$_sqr->execute();
    #if($_sqr->rows){
        #$_ref = $_sqr->fetchrow_hashref();
        #$_sqr = $$self{'db'}->prepare("update follows set 
            #$_follow_qr_str
            #where id=$$_ref{'id'}");
        #$_sqr->execute;
    #}else{
        #$_rows = $$self{'db'}->do("insert into follows set
            #user_id =  $user_id,
            #host_id =  $host_id,
            #$_follow_qr_str
            #");
    #}
#}


sub temp_get_bj{
	my ($self) = @_;

	my $_sqr;
	$_sqr = $$self{'db'}->prepare("select id,name from users where area like '北京%' and vip_verify=0");
	$_sqr->execute();
	my $_ref;
	my $_line=0;
	while ( $_ref = $_sqr->fetchrow_hashref()){
		$$_ref{'name'} =~ s/(^")|("$)//g;
		print '@'.$$_ref{'name'}." ";         
		$_line++;
		if( $_line%10==0){ print "\n";}
	}
}


1;






